<template>
  <div class="content">
    <div class="top_button">
      <span class="el-icon-search" ></span>
      <el-button @click="flush()" size="small">刷新</el-button>
      <el-button type="primary" name="but-insert" id="but-insert" @click="Insert()"size="small" >新增</el-button>
      <el-button type="danger" name="but-delete" id="but-delete" @click="delBySelect()" size="small">删除</el-button>
    </div>

<!-- 新增优先级弹窗 -->
  <div class="window_insert">
    <el-dialog title="新增优先级" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form  :model="insertForm" :rules="rules" ref="insertForm" >
        <el-form-item label="编号" prop='defectpriorityNum' >
          <el-input v-model="insertForm.defectpriorityNum" ></el-input>
        </el-form-item>

        <el-form-item label="优先级"  prop='defectpriorityName'>
          <el-input v-model="insertForm.defectpriorityName" ></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">

        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="insertInto()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  <!-- 修改优先级弹窗 -->
    <div class="window_update">
      <el-dialog title="修改优先级" :visible.sync="dialogFormVisible2" :before-close="handleClose">
        <el-form :model="updateForm" :rules="rules" ref="updateForm">
          <el-form-item label="编号" prop='defectpriorityNum'>
            <el-input v-model="updateForm.defectpriorityNum" ></el-input>
          </el-form-item>

          <el-form-item label="优先级" prop="defectpriorityName" >
            <el-input v-model="updateForm.defectpriorityName" ></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible2 = false" >取 消</el-button>
          <el-button type="primary" @click="updateInto()" >确 定</el-button>
        </div>
      </el-dialog>
    </div>


    <div class="middle">

      <div>
        <el-table  style="width: 1001" :data="list" @selection-change="handleSelectionChange">
        	<el-table-column type="selection" width="200px" >
        	</el-table-column>
        	<el-table-column prop="defectpriorityNum" label="编号" width="200px">
        	</el-table-column>
        	<el-table-column prop="defectpriorityName" label="优先级" width="200px">
        	</el-table-column>
        	<el-table-column prop="defectpriorityNewState" label="状态" width="200px">
        	</el-table-column>
        	<el-table-column label="操作"  width="200px">

        			<template slot-scope="scope">
        			  <el-button  type="text" size="small" @click="ichange(scope.row)">修改</el-button>
              <el-button  type="text" size="small" @click="ban(scope.row)" ><div v-if="scope.row.defectpriorityNewState=='有效'">禁用</div></el-button>
              <el-button type="text" size="small" @click="start(scope.row)"><div v-if="scope.row.defectpriorityNewState=='禁用'">启用</div></el-button>
        			  <el-button type="text" size="small" @click="delById(scope.row.defectpriorityId)">删除</el-button>
        			</template>
        	</el-table-column>
        </el-table>

      </div>


    </div>
  </div>
</template>

<script>
  export default {
    name:"Projectpriority",
    data(){
      return{

        rules:{
           defectpriorityNum:[{required: true, message: '请输入编号', trigger: 'blur'}],
          defectpriorityName:[{required: true, message: '请输入状态', trigger: 'blur'}]
        },

      list:[{
        defectpriorityId:null,
        defectpriorityNum: null,
        defectpriorityName:null,
        defectpriorityNewState:''
      }],
       dialogFormVisible: false,
       dialogFormVisible2:false,

      insertForm:{
        defectpriorityNum: null,
        defectpriorityName:null,
        defectpriorityState:1
      },

      updateForm:{
        defectpriorityId:null,
        defectpriorityNum: null,
        defectpriorityName:null,
        defectpriorityState:1
      },
      //批量删除
      many:[]


      }
    },
    methods:{
      
      handleClose(done) {
              this.$confirm('确认关闭？')
                .then(_ => {
                  done();
                })
                .catch(_ => {});
            },
      
        //刷新整个页面
      flush(){
          location.reload()
         },

         //查询所有优先级
      findAll(){

          this.$axios.post("/defectpriority/findAll").then(resp=>{
            this.list=resp.data.data
            console.log(this.list)
             })
         },
      Insert(){
        this.dialogFormVisible=true
      },
      //新增
      insertInto(){
        console.log(this.insertForm)

    this.$refs.insertForm.validate((data) => {

              if(data){
            this.$axios.post("/defectpriority/addPriority",this.insertForm).then(resp=>{
                console.log(resp.data)
              if (resp.data.code==200) {
                  this.$message({
                    message: '新增成功',
                    type: 'success'
                  });
                  this.dialogFormVisible=false
                  this.findAll()
                } else {
                  this.$message.error('新增失败')
                }
                  })
          }else{
            this.$message.error("请输入所有字段")
        }

})





      },
      //批量选择
      handleSelectionChange(val){

       var array=[];
       for(var i=0;i<val.length;i++){
         array[i]=val[i].defectpriorityId;

       }
       this.many=array;
      },
      //批量删除
      delBySelect(){
        var list=this.many

        if(list.length !=0){

          this.$confirm('是否确定删除?', '提示', {
          	confirmButtonText: '确定',
          	cancelButtonText: '取消',
          	type: 'warning'
          }).then(() => {
          this.$axios.post("/defectpriority/delectMany",this.many).then(resp=>{


            if(resp.data.code==200){
              this.$message({
              		type: 'success',
              		message: '删除成功!'
              	});
                this.findAll()
              }else{
              	this.$message.error('删除失败')
              }
            })
          })
          }
      },
      //修改优先级
      ichange(priority){
        this.updateForm.defectpriorityNum=priority.defectpriorityNum
        this.updateForm.defectpriorityName=priority.defectpriorityName
        this.dialogFormVisible2=true
        this.updateForm.defectpriorityId=priority.defectpriorityId
      },


      updateInto(){
        this.$refs.updateForm.validate((data) => {
          if(data){
            this.$axios.post("/defectpriority/update",this.updateForm).then(resp=>{
              if (resp.data.code == 200) {
              	this.$message({
              		message: '更新成功',
              		type: 'success'
              	});
              this.findAll()
              this.dialogFormVisible2=false
              } else {
              	this.$message.error('更新失败')
              }
            })
          }else{
            this.$message.error("请输入所有字段")
          }

        })


      },

      //单个删除
      delById(id){

        this.$confirm('是否确定删除?', '提示', {
        	confirmButtonText: '确定',
        	cancelButtonText: '取消',
        	type: 'warning'
        }).then(() => {
        this.$axios.post("/defectpriority/delectbyid/"+id).then(resp=>{
          if (resp.data.code == 200) {
          	this.$message({
          		message: '删除成功',
          		type: 'success'
          	});
          this.findAll()
          } else {
          	this.$message.error('删除失败')
          }
        })
        })
      },

      //禁用
      ban(list){

        this.$axios.post("/defectpriority/changeState",list).then(resp=>{

          if (resp.data.code == 200) {
            	this.$message({
            		message: '禁用成功',
            		type: 'success'
            	});
            this.findAll()
            } else {
            	this.$message.error('禁用失败')
            }
          })
      },
      //启用
      start(list){

        this.$axios.post("/defectpriority/changeState",list).then(resp=>{

          if (resp.data.code == 200) {
            	this.$message({
            		message: '启用成功',
            		type: 'success'
            	});
            this.findAll()
            } else {
            	this.$message.error('启用失败')
            }
          })

    }
    },
    //页面初始化
      created(){
        this.findAll()
      }
  }

</script>

<style>
  .el-icon-search{
    position: relative;
    left: -1060px;
  }

.top_button{
  background-color: #F2F2F2;
  text-align: right;
}
</style>
